#include "DirectLOL/CollisionDetection/collisions.h"

Collisions::Collisions()
{
}


int Collisions::BSphereToBSphere(BoundingSphere * b1, BoundingSphere * b2)
{
    float normelol= (*b1->translation+*b1->isoBarycentre- * b2->translation+*b2->isoBarycentre).Norme();
    if(normelol< (b1->rayon + b2->rayon) )
    {
        return 1;
    }
    return 0;
}

int Collisions::AABBtoBSphere(AABB * a1 , BoundingSphere * b2)
{
    float * center=(*b2->translation+*b2->isoBarycentre).Array();
    float * aabbMin=a1->min->Array();
    float * aabbMax=a1->max->Array();

    float s, d = 0;
    //find the square of the distance
    //from the sphere to the box
    for( long i=0 ; i<3 ; i++ )
    {
        if( center[i] < aabbMin[i] )
        {
            s = center[i] -aabbMin[i];
            d += s*s;
        }
        else if( center[i] > aabbMax[i] )
        {
            s = center[i] - aabbMax[i];
            d += s*s;
        }
    }

   return d <= b2->rayon*b2->rayon;

}

int Collisions::AABBtoAABB(AABB *a1, AABB * a2)
{
    /*! Implémentation de l'algorithme de SAT (separation axis algorithme
     *  Normalement, on teste les normales des arretes des 2 faces, que l'on projete sur x et y et z
     *  La, on se contente de projeter sur x et y
     */

    Vectorf * bMin= new Vectorf(*a2->min );
    *bMin=*bMin+*a2->translationT1;

    Vectorf * bMax= new Vectorf(*a2->max);
    *bMax=*bMax+*a2->translationT1;


    Vectorf * aMax = new Vectorf(*a1->max);
    *aMax= *aMax+*a1->translationT1;

    Vectorf * aMin = new Vectorf(*a1->min);
    *aMin= *aMin+*a1->translationT1;

    float * aMinf=aMin->Array();
    float * aMaxf=aMax->Array();

    float * bMinf= bMin->Array();
    float * bMaxf= bMax->Array();

    int overlap=1;

    for(int i=0;i<3;i++)
    {
        if( bMinf[i] > aMinf[i] && bMinf[i] < aMaxf[i])
        {

        }
        else if( bMaxf[i] > aMinf[i] && bMaxf[i] < aMaxf[i])
        {

        }
        else if(bMinf[i]<aMinf[i] && bMaxf[i]>aMaxf[i])
        {

        }
        else
        {
            overlap=0;
        }
    }
    return overlap;
}
